﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Main_Inventory_PurchaseReturn : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string strsql = "";
    int controlCounterBillLevel = 0;
    #region Global Variable for Tax At Bill Level
  
    List<string> LstCountBillLevel;
    List<string> LstItemBillLevel;
    List<string> LstItemIdBillLevel;
    List<string> LstUnitBillLevel;
    List<string> LstUnitIdBillLevel;
    List<string> LstQtyBillLevel;
    List<string> LstRateBillLevel;
    List<string> LstButtonBillLevel;
    List<string> LstLocationBillLevel;
    List<string> LstTaxRateBillLevel;
    List<string> LstTaxAmtBillLevel;
    List<string> LstGrossAmtBillLevel;
    List<string> LstTaxTypeBillLevel;
    List<string> LstTaxAmountBillLevel;
    List<string> LstTotalAmountBillLevel;
    #endregion    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            #region Initialized list for bill level tax

            LstCountBillLevel = new List<string>();
            ViewState["LstCountBillLevel"] = LstCountBillLevel;

            LstItemBillLevel = new List<string>();
            ViewState["LstItemBillLevel"] = LstItemBillLevel;

            LstItemIdBillLevel = new List<string>();
            ViewState["LstItemIdBillLevel"] = LstItemIdBillLevel;

            LstUnitBillLevel = new List<string>();
            ViewState["LstUnitBillLevel"] = LstUnitBillLevel;

            LstUnitIdBillLevel = new List<string>();
            ViewState["LstUnitIdBillLevel"] = LstUnitIdBillLevel;

            LstQtyBillLevel = new List<string>();
            ViewState["LstQtyBillLevel"] = LstQtyBillLevel;

            LstRateBillLevel = new List<string>();
            ViewState["LstRateBillLevel"] = LstRateBillLevel;

            LstButtonBillLevel = new List<string>();
            ViewState["LstButtonBillLevel"] = LstButtonBillLevel;

            LstLocationBillLevel = new List<string>();
            ViewState["LstLocationBillLevel"] = LstLocationBillLevel;

            LstTaxRateBillLevel = new List<string>();
            ViewState["LstTaxRateBillLevel"] = LstTaxRateBillLevel;

            LstTaxAmtBillLevel = new List<string>();
            ViewState["LstTaxAmtBillLevel"] = LstTaxAmtBillLevel;

            LstGrossAmtBillLevel = new List<string>();
            ViewState["LstGrossAmtBillLevel"] = LstGrossAmtBillLevel;

            LstTaxTypeBillLevel = new List<string>();
            ViewState["LstTaxTypeBillLevel"] = LstTaxTypeBillLevel;

            LstTaxAmountBillLevel = new List<string>();
            ViewState["LstTaxAmountBillLevel"] = LstTaxAmountBillLevel;

            LstTotalAmountBillLevel = new List<string>();
            ViewState["LstTotalAmountBillLevel"] = LstTotalAmountBillLevel;
            #endregion
            Session["DTPurchaseReturn"] = null;
            BindSuppliers();
            BindTranFormat();
            BindTaxGroup();
            IsTranFormatTaxAtItemLevel(ddlFormat.SelectedValue);
            txtDate.Text = string.Format("{0:dd/MM/yyyy}", Convert.ToDateTime(System.DateTime.Now)).ToString();//Set Currunt Date to text box
            CheckFormatAndCustomerIsNull();
            BindTaxAndSaleLedger();
        }
        BindSaleReturnItems();
        BindJavaScriptBillLevel();
    }
    protected void BindTranFormat()//Bind Transaction format 
    {
        SqlParameter[] sparam =
                            {
                                new SqlParameter("@TranType","6")
                            };
        DataTable DT = DU.getdatausingprocedure("USP_IVR_GetTransactionFormat", sparam);
        if (DT.Rows.Count > 0)
        {
            ddlFormat.DataSource = DT;
            ddlFormat.DataTextField = "FormatName";
            ddlFormat.DataValueField = "Tfoid";
            ddlFormat.DataBind();
            ddlFormat.SelectedIndex = 0;
            Bind_VoucherNo();
            BindDdlTaxType();
            IsTranFormatTaxAtItemLevel(ddlFormat.SelectedValue);

            if (hidTranFormatType.Value.ToString() == "True")
            {
                trItemLevel.Visible = true;
                trBillLevel.Visible = false;
            }
            else if (hidTranFormatType.Value.ToString() == "False")
            {
                trItemLevel.Visible = false;
                trBillLevel.Visible = true;
            }
        }
    }

    protected void Bind_VoucherNo()//Get And Bind Next Voucher number to textbox.
    {
        if (ddlFormat.SelectedValue != "0")
        {
            SqlParameter[] sparam =
                                 {
                                     new SqlParameter("@TFOID",ddlFormat.SelectedValue),
                                     new SqlParameter("@FINYEAR",Session["FINYEAR"].ToString())
                                 };
            DataTable DTNum = DU.getdatausingprocedure("USP_IVR_GetMaxVoucherNumber", sparam);
            if (DTNum.Rows.Count > 0)
            {
                txtPrefix.Text = DTNum.Rows[0]["NUMPREFIX"].ToString();
                txtVoucher.Text = DTNum.Rows[0]["NUMSTART"].ToString();
                txtSuffix.Text = DTNum.Rows[0]["NUMSUFFIX"].ToString();
            }
        }
    }

    protected void BindSuppliers()//Bind Suppliers to ddlSupplier
    {
        SqlParameter[] sparam ={
                                   new SqlParameter("@TRANTYPE","4")
                               };
        DataTable DT = DU.getdatausingprocedure("USP_IVR_GetLedgerForSalesPurchaseReturn", sparam);
        if (DT.Rows.Count > 0)
        {
            ddlSupplier.DataSource = DT;
            ddlSupplier.DataTextField = "LNAME";
            ddlSupplier.DataValueField = "LID";
            ddlSupplier.DataBind();
        }
    }

    protected void BindTaxGroup()//Bind Tax Group to dropdownlist
    {
        DataTable DT = DU.getdatausingprocedure("USP_ACC_GetPurchaseMainClass");
        if (DT.Rows.Count > 0)
        {
            ddlTaxGroup.DataSource = DT;
            ddlTaxGroup.DataTextField = "MAINCLASSNAME";
            ddlTaxGroup.DataValueField = "MAINCLASSID";
            ddlTaxGroup.DataBind();
            ddlTaxGroup.Items.Insert(0, new ListItem("", "0"));
        }
    }

    protected void ddlFormat_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            Session["DTPurchaseReturn"] = null;
            Bind_VoucherNo();
            IsTranFormatTaxAtItemLevel(ddlFormat.SelectedValue);
            BindDdlTaxType();
            BindSaleReturnItems();
            //ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "CreateEvent();", true);
            BindTaxAndSaleLedger();
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
    }

    protected void IsTranFormatTaxAtItemLevel(string strTranFormat)//Check Transaction format Tax at item Level OR Not 
    {
        if (strTranFormat.ToString() != string.Empty)
        {
            strsql = "SELECT TAXATITEM FROM IVR_TRANFORMAT WHERE TFOID='" + strTranFormat.ToString() + "'";
            DataTable DTFormat = DU.getdata(strsql);
            if (DTFormat.Rows.Count > 0)
            {
                ///if True then tax at item level
                ///else Tax at Bill Level
                if (DTFormat.Rows[0]["TAXATITEM"].ToString() == "True")
                {
                    hidTranFormatType.Value = "True";
                    ddlVatBillLevel.Visible = false;
                    ddlVatBillLevel.Enabled = false;
                    panelSaleBillLevel.Visible = false;
                    ddlSaleLedger.Visible = false;
                    ddlSaleLedger.Enabled = false;
                    ddlSaleTaxLedger.Visible = false;
                    ddlSaleTaxLedger.Enabled = false;
                    trBillLevel.Visible = false;
                    trItemLevel.Visible = true;
                }
                else if (DTFormat.Rows[0]["TAXATITEM"].ToString() == "False")
                {
                    hidTranFormatType.Value = "False";
                    ddlVatBillLevel.Visible = true;
                    ddlVatBillLevel.Enabled = true;
                    panelSaleBillLevel.Visible = true;
                    ddlSaleLedger.Visible = true;
                    ddlSaleLedger.Enabled = true;
                    ddlSaleTaxLedger.Visible = true;
                    ddlSaleTaxLedger.Enabled = true;
                    trBillLevel.Visible = true;
                    trItemLevel.Visible = false;
                }
            }
        }
    }       

    protected void BindAllTaxDropdownlist(DropDownList ddlTaxType)//Function for bind subtax group
    {

        strsql = "select SUBCLASSNAME,SUBCLASSID from UVW_ACC_TaxMaster where MAINCLASSID ='" + ddlTaxGroup.SelectedValue + "'";
        DataTable DT = DU.getdata(strsql);
        if (DT.Rows.Count > 0)
        {
            ddlTaxType.DataSource = DT;
            ddlTaxType.DataTextField = "SUBCLASSNAME";
            ddlTaxType.DataValueField = "SUBCLASSID";
            ddlTaxType.DataBind();
            ddlTaxType.Items.Insert(0, new ListItem("-Select-", "0"));
        }
        else
        {
            ddlTaxType.Items.Clear();
        }
    }

    protected void BindDdlTaxType()//Assign Tax type to dropdownlist control
    {
        if (hidTranFormatType.Value == "True")
        {
            int intCount = Convert.ToInt32(hidControlBillLevel.Value);
            if (intCount > 0)
            {
                for (int i = 1; i <= intCount; i++)
                {
                    DropDownList ddlTaxType = (DropDownList)tblBillLevel.Rows[i].FindControl("ddlTaxType" + i.ToString());
                    BindAllTaxDropdownlist(ddlTaxType);
                }
            }
        }
    }

    protected void ddlTaxGroup_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        if (hidTranFormatType.Value == "False")
        {
            BindAllTaxDropdownlist(ddlVatBillLevel);
        }
        else if (hidTranFormatType.Value == "True")
        {
            BindDdlTaxType();
        }
    }

    protected void BindTaxLedger(DropDownList ddl)//Common function for bind Tax ledger for transaction
    {
        DataTable DT = DU.getdatausingprocedure("USP_IVR_GetTaxLedgerforPurchaseInvoice");
        if (DT.Rows.Count > 0)
        {
            ddl.DataSource = DT;
            ddl.DataTextField = "Lname";
            ddl.DataValueField = "LID";
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("- Select Ledger -", "0"));
        }
        else
        {
            ddl.Items.Clear();
        }
    }

    protected void BindSalesLedger(DropDownList ddl)//Common function for bind Sales ledger for transaction
    {
        DataTable DT = DU.getdatausingprocedure("USP_IVR_GetPurchaseLedgerforPurchaseInvoice");
        if (DT.Rows.Count > 0)
        {
            ddl.DataSource = DT;
            ddl.DataTextField = "Lname";
            ddl.DataValueField = "LID";
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("- Select Ledger -", "0"));
        }
        else
        {
            ddl.Items.Clear();
        }
    }

    protected void BindTaxAndSaleLedger()//Bind Tax & Sale Ledger to dropdownlist control
    {
        if (hidTranFormatType.Value.ToString() == "True")
        {
            int count = Convert.ToInt32(hidControlBillLevel.Value);
            if (count > 0)
            {
                for (int i = 1; i <= count; i++)
                {
                    DropDownList ddlSaleTaxLedger = (DropDownList)tblBillLevel.Rows[i].FindControl("ddlSaleTaxLedger" + i.ToString());
                    DropDownList ddlSaleLedger = (DropDownList)tblBillLevel.Rows[i].FindControl("ddlSaleLedger" + i.ToString());
                    BindTaxLedger(ddlSaleTaxLedger);
                    BindSalesLedger(ddlSaleLedger);
                }
            }
        }
        else if (hidTranFormatType.Value.ToString() == "False")
        {
            BindSalesLedger(ddlSaleLedger);
            BindTaxLedger(ddlSaleTaxLedger);
        }
    }

    protected void btnCreateItem_Click(object sender, EventArgs e)//A button used for  Create event from javascript.
    {
        BindSaleReturnItems();
    }  
    
    protected void AddMoreBillLevel()//Add more row for tax at bill level
    {
        if (hidTranFormatType.Value == "True")
        {
            trBillLevel.Visible = false;
            trItemLevel.Visible = true;
            controlCounterBillLevel++;
            HtmlTableRow HTRow = new HtmlTableRow();

            HtmlTableCell HTCell1 = new HtmlTableCell();
            TextBox txtItemBillLevel = new TextBox();
            txtItemBillLevel.ID = "txtItemBillLevel" + controlCounterBillLevel.ToString();
            txtItemBillLevel.CssClass = "input";
            txtItemBillLevel.Width = Unit.Percentage(98);
            HiddenField hidItemIdBillLevel = new HiddenField();
            hidItemIdBillLevel.ID = "hidItemIdBillLevel" + controlCounterBillLevel.ToString();
            HTCell1.Controls.Add(txtItemBillLevel);
            HTCell1.Controls.Add(hidItemIdBillLevel);
            //LstCountBillLevel.Add(controlCounterBillLevel.ToString());
            //LstItemBillLevel.Add(txtItemBillLevel.ID);
            //LstItemIdBillLevel.Add(hidItemIdBillLevel.ID);


            HtmlTableCell HTCell2 = new HtmlTableCell();
            TextBox txtUnitBillLevel = new TextBox();
            txtUnitBillLevel.ID = "txtUnitBillLevel" + controlCounterBillLevel.ToString();
            txtUnitBillLevel.CssClass = "input";
            txtUnitBillLevel.Width = Unit.Percentage(98);
            HiddenField hidUnitIdBillLevel = new HiddenField();
            hidUnitIdBillLevel.ID = "hidUnitIdBillLevel" + controlCounterBillLevel.ToString();
            HTCell2.Controls.Add(txtUnitBillLevel);
            HTCell2.Controls.Add(hidUnitIdBillLevel);
            //LstUnitBillLevel.Add(txtUnitBillLevel.ID);
            //LstUnitIdBillLevel.Add(hidUnitIdBillLevel.ID);

            HtmlTableCell HTCell3 = new HtmlTableCell();
            TextBox txtQuantityBillLevel = new TextBox();
            txtQuantityBillLevel.ID = "txtQuantityBillLevel" + controlCounterBillLevel.ToString();
            txtQuantityBillLevel.CssClass = "inputnumber";
            txtQuantityBillLevel.Width = Unit.Percentage(98);
            HTCell3.Controls.Add(txtQuantityBillLevel);
            //LstQtyBillLevel.Add(txtQuantityBillLevel.ID);

            HtmlTableCell HTCell4 = new HtmlTableCell();
            Button btnQtyBillLevel = new Button();
            btnQtyBillLevel.ID = "btnQtyBillLevel" + controlCounterBillLevel.ToString();
            btnQtyBillLevel.Text = "...";
            btnQtyBillLevel.CssClass = "button";
            btnQtyBillLevel.Width = Unit.Percentage(98);
            HiddenField hidLocationBillLevel = new HiddenField();
            hidLocationBillLevel.ID = "hidLocationBillLevel" + controlCounterBillLevel.ToString();
            HTCell4.Controls.Add(btnQtyBillLevel);
            HTCell4.Controls.Add(hidLocationBillLevel);
            //LstButtonBillLevel.Add(btnQtyBillLevel.ID);
            //LstLocationBillLevel.Add(hidLocationBillLevel.ID);

            HtmlTableCell HTCell5 = new HtmlTableCell();
            TextBox txtRateBillLevel = new TextBox();
            txtRateBillLevel.ID = "txtRateBillLevel" + controlCounterBillLevel.ToString();
            txtRateBillLevel.CssClass = "inputnumber";
            txtRateBillLevel.Width = Unit.Percentage(98);
            txtRateBillLevel.Attributes.Add("onblur", "return CalculateAmtBillLevel();");
            HiddenField hidTaxRateBillLevel = new HiddenField();
            hidTaxRateBillLevel.ID = "hidTaxRateBillLevel" + controlCounterBillLevel.ToString();
            HiddenField hidTaxAmtBillLevel = new HiddenField();
            hidTaxAmtBillLevel.ID = "hidTaxAmtBillLevel" + controlCounterBillLevel.ToString();
            HTCell5.Controls.Add(txtRateBillLevel);
            HTCell5.Controls.Add(hidTaxRateBillLevel);
            HTCell5.Controls.Add(hidTaxAmtBillLevel);
            //LstRateBillLevel.Add(txtRateBillLevel.ID);
            //LstTaxRateBillLevel.Add(hidTaxRateBillLevel.ID);
            //LstTaxAmtBillLevel.Add(hidTaxAmtBillLevel.ID);


            HtmlTableCell HTCell6 = new HtmlTableCell();
            TextBox txtGrossAmtBillLevel = new TextBox();
            txtGrossAmtBillLevel.ID = "txtGrossAmtBillLevel" + controlCounterBillLevel.ToString();
            txtGrossAmtBillLevel.CssClass = "inputnumber";
            txtGrossAmtBillLevel.Width = Unit.Percentage(98);
            HTCell6.Controls.Add(txtGrossAmtBillLevel);
            //LstGrossAmtBillLevel.Add(txtGrossAmtBillLevel.ID);


            HtmlTableCell HTCell7 = new HtmlTableCell();
            DropDownList ddlTaxType = new DropDownList();
            ddlTaxType.ID = "ddlTaxType" + controlCounterBillLevel.ToString();
            ddlTaxType.CssClass = "dropdown";
            ddlTaxType.Width = Unit.Percentage(98);
            ddlTaxType.AutoPostBack = true;
            ddlTaxType.SelectedIndexChanged += new EventHandler(ddlTaxTypeBillLevel_OnSelectedIndexChanged);
            HTCell7.Controls.Add(ddlTaxType);
            //LstTaxTypeBillLevel.Add(ddlTaxType.ID);


            HtmlTableCell HTCell8 = new HtmlTableCell();
            TextBox txtTaxAmountBillLevel = new TextBox();
            txtTaxAmountBillLevel.ID = "txtTaxAmountBillLevel" + controlCounterBillLevel.ToString();
            txtTaxAmountBillLevel.CssClass = "inputnumber";
            txtTaxAmountBillLevel.Width = Unit.Percentage(98);
            HTCell8.Controls.Add(txtTaxAmountBillLevel);
            //LstTaxAmountBillLevel.Add(txtTaxAmountBillLevel.ID);


            HtmlTableCell HTCell9 = new HtmlTableCell();
            TextBox txtTotalAmountBillLevel = new TextBox();
            txtTotalAmountBillLevel.ID = "txtTotalAmountBillLevel" + controlCounterBillLevel.ToString();
            txtTotalAmountBillLevel.CssClass = "inputnumber";
            txtTotalAmountBillLevel.Width = Unit.Percentage(98);
            HTCell9.Controls.Add(txtTotalAmountBillLevel);

            HtmlTableCell HTCell10 = new HtmlTableCell();
            DropDownList ddlSaleLedger = new DropDownList();
            ddlSaleLedger.ID = "ddlSaleLedger" + controlCounterBillLevel.ToString();
            ddlSaleLedger.CssClass = "dropdown";
            ddlSaleLedger.Width = Unit.Percentage(98);
            BindSalesLedger(ddlSaleLedger);
            HTCell10.Controls.Add(ddlSaleLedger);

            HtmlTableCell HTCell11 = new HtmlTableCell();
            DropDownList ddlSaleTaxLedger = new DropDownList();
            ddlSaleTaxLedger.ID = "ddlSaleTaxLedger" + controlCounterBillLevel.ToString();
            ddlSaleTaxLedger.CssClass = "dropdown";
            ddlSaleTaxLedger.Width = Unit.Percentage(98);
            BindTaxLedger(ddlSaleTaxLedger);
            HTCell11.Controls.Add(ddlSaleTaxLedger);

            //LstTotalAmountBillLevel.Add(txtTotalAmountBillLevel.ID);


            HTRow.Cells.Add(HTCell1);
            HTRow.Cells.Add(HTCell2);
            HTRow.Cells.Add(HTCell3);
            HTRow.Cells.Add(HTCell4);
            HTRow.Cells.Add(HTCell5);
            HTRow.Cells.Add(HTCell6);
            HTRow.Cells.Add(HTCell7);
            HTRow.Cells.Add(HTCell8);
            HTRow.Cells.Add(HTCell9);
            HTRow.Cells.Add(HTCell10);
            HTRow.Cells.Add(HTCell11);

            tblBillLevel.Rows.Add(HTRow);

            //ViewState["LstCountBillLevel"] = LstCountBillLevel;
            //ViewState["LstItemBillLevel"] = LstItemBillLevel;
            //ViewState["LstItemIdBillLevel"] = LstItemIdBillLevel;
            //ViewState["LstUnitBillLevel"] = LstUnitBillLevel;
            //ViewState["LstUnitIdBillLevel"] = LstUnitIdBillLevel;
            //ViewState["LstQtyBillLevel"] = LstQtyBillLevel;
            //ViewState["LstRateBillLevel"] = LstRateBillLevel;
            //ViewState["LstButtonBillLevel"] = LstButtonBillLevel;
            //ViewState["LstLocationBillLevel"] = LstLocationBillLevel;
            //ViewState["LstTaxRateBillLevel"] = LstTaxRateBillLevel;
            //ViewState["LstTaxAmtBillLevel"] = LstTaxAmtBillLevel;
            //ViewState["LstGrossAmtBillLevel"] = LstGrossAmtBillLevel;
            //ViewState["LstTaxTypeBillLevel"] = LstTaxTypeBillLevel;
            //ViewState["LstTaxAmountBillLevel"] = LstTaxAmountBillLevel;
            //ViewState["LstTotalAmountBillLevel"] = LstTotalAmountBillLevel;

            hidControlBillLevel.Value = controlCounterBillLevel.ToString();
        }
        else if (hidTranFormatType.Value == "False")
        {
            trBillLevel.Visible = true;
            trItemLevel.Visible = false;
            controlCounterBillLevel++;
            HtmlTableRow HTRow = new HtmlTableRow();

            HtmlTableCell HTCell1 = new HtmlTableCell();
            TextBox txtItemBillLevel = new TextBox();
            txtItemBillLevel.ID = "txtItemBillLevel" + controlCounterBillLevel.ToString();
            txtItemBillLevel.CssClass = "input";
            txtItemBillLevel.Width = Unit.Percentage(98);
            HiddenField hidItemIdBillLevel = new HiddenField();
            hidItemIdBillLevel.ID = "hidItemIdBillLevel" + controlCounterBillLevel.ToString();
            HTCell1.Controls.Add(txtItemBillLevel);
            HTCell1.Controls.Add(hidItemIdBillLevel);
            //LstCountBillLevel.Add(controlCounterBillLevel.ToString());
            //LstItemBillLevel.Add(txtItemBillLevel.ID);
            //LstItemIdBillLevel.Add(hidItemIdBillLevel.ID);


            HtmlTableCell HTCell2 = new HtmlTableCell();
            TextBox txtUnitBillLevel = new TextBox();
            txtUnitBillLevel.ID = "txtUnitBillLevel" + controlCounterBillLevel.ToString();
            txtUnitBillLevel.CssClass = "input";
            txtUnitBillLevel.Width = Unit.Percentage(98);
            HiddenField hidUnitIdBillLevel = new HiddenField();
            hidUnitIdBillLevel.ID = "hidUnitIdBillLevel" + controlCounterBillLevel.ToString();
            HTCell2.Controls.Add(txtUnitBillLevel);
            HTCell2.Controls.Add(hidUnitIdBillLevel);
            //LstUnitBillLevel.Add(txtUnitBillLevel.ID);
            //LstUnitIdBillLevel.Add(hidUnitIdBillLevel.ID);

            HtmlTableCell HTCell3 = new HtmlTableCell();
            TextBox txtQuantityBillLevel = new TextBox();
            txtQuantityBillLevel.ID = "txtQuantityBillLevel" + controlCounterBillLevel.ToString();
            txtQuantityBillLevel.CssClass = "inputnumber";
            txtQuantityBillLevel.Width = Unit.Percentage(98);
            HTCell3.Controls.Add(txtQuantityBillLevel);
            //LstQtyBillLevel.Add(txtQuantityBillLevel.ID);

            HtmlTableCell HTCell4 = new HtmlTableCell();
            Button btnQtyBillLevel = new Button();
            btnQtyBillLevel.ID = "btnQtyBillLevel" + controlCounterBillLevel.ToString();
            btnQtyBillLevel.Text = "...";
            btnQtyBillLevel.CssClass = "button";
            btnQtyBillLevel.Width = Unit.Percentage(98);
            HiddenField hidLocationBillLevel = new HiddenField();
            hidLocationBillLevel.ID = "hidLocationBillLevel" + controlCounterBillLevel.ToString();
            HTCell4.Controls.Add(btnQtyBillLevel);
            HTCell4.Controls.Add(hidLocationBillLevel);
            //LstButtonBillLevel.Add(btnQtyBillLevel.ID);
            //LstLocationBillLevel.Add(hidLocationBillLevel.ID);

            HtmlTableCell HTCell5 = new HtmlTableCell();
            TextBox txtRateBillLevel = new TextBox();
            txtRateBillLevel.ID = "txtRateBillLevel" + controlCounterBillLevel.ToString();
            txtRateBillLevel.CssClass = "inputnumber";
            txtRateBillLevel.Width = Unit.Percentage(98);
            txtRateBillLevel.Attributes.Add("onblur", "return CalculateAmtBillLevel();");
            HiddenField hidTaxRateBillLevel = new HiddenField();
            hidTaxRateBillLevel.ID = "hidTaxRateBillLevel" + controlCounterBillLevel.ToString();
            HiddenField hidTaxAmtBillLevel = new HiddenField();
            hidTaxAmtBillLevel.ID = "hidTaxAmtBillLevel" + controlCounterBillLevel.ToString();
            HTCell5.Controls.Add(txtRateBillLevel);
            HTCell5.Controls.Add(hidTaxRateBillLevel);
            HTCell5.Controls.Add(hidTaxAmtBillLevel);
            //LstRateBillLevel.Add(txtRateBillLevel.ID);
            //LstTaxRateBillLevel.Add(hidTaxRateBillLevel.ID);
            //LstTaxAmtBillLevel.Add(hidTaxAmtBillLevel.ID);

            HtmlTableCell HTCell6 = new HtmlTableCell();
            TextBox txtTotalAmountBillLevel = new TextBox();
            txtTotalAmountBillLevel.ID = "txtTotalAmountBillLevel" + controlCounterBillLevel.ToString();
            txtTotalAmountBillLevel.CssClass = "inputnumber";
            txtTotalAmountBillLevel.Width = Unit.Percentage(98);
            HTCell6.Controls.Add(txtTotalAmountBillLevel);
            //LstTotalAmountBillLevel.Add(txtTotalAmountBillLevel.ID);


            HTRow.Cells.Add(HTCell1);
            HTRow.Cells.Add(HTCell2);
            HTRow.Cells.Add(HTCell3);
            HTRow.Cells.Add(HTCell4);
            HTRow.Cells.Add(HTCell5);
            HTRow.Cells.Add(HTCell6);

            tblBillLevel.Rows.Add(HTRow);

            //ViewState["LstCountBillLevel"] = LstCountBillLevel;
            //ViewState["LstItemBillLevel"] = LstItemBillLevel;
            //ViewState["LstItemIdBillLevel"] = LstItemIdBillLevel;
            //ViewState["LstUnitBillLevel"] = LstUnitBillLevel;
            //ViewState["LstUnitIdBillLevel"] = LstUnitIdBillLevel;
            //ViewState["LstQtyBillLevel"] = LstQtyBillLevel;
            //ViewState["LstRateBillLevel"] = LstRateBillLevel;
            //ViewState["LstButtonBillLevel"] = LstButtonBillLevel;
            //ViewState["LstLocationBillLevel"] = LstLocationBillLevel;
            //ViewState["LstTaxRateBillLevel"] = LstTaxRateBillLevel;
            //ViewState["LstTaxAmtBillLevel"] = LstTaxAmtBillLevel;
            //ViewState["LstTotalAmountBillLevel"] = LstTotalAmountBillLevel;

            hidControlBillLevel.Value = controlCounterBillLevel.ToString();
        }
    }

    protected void OverrideAddMoreBillLevel()//Override View State for Added More Row to bill level tax
    {
        LstCountBillLevel = (List<string>)ViewState["LstCountBillLevel"];
        LstItemIdBillLevel = (List<string>)ViewState["LstItemIdBillLevel"];
        LstItemBillLevel = (List<string>)ViewState["LstItemBillLevel"];
        LstUnitBillLevel = (List<string>)ViewState["LstUnitBillLevel"];
        LstUnitIdBillLevel = (List<string>)ViewState["LstUnitIdBillLevel"];
        LstQtyBillLevel = (List<string>)ViewState["LstQtyBillLevel"];
        LstRateBillLevel = (List<string>)ViewState["LstRateBillLevel"];
        LstButtonBillLevel = (List<string>)ViewState["LstButtonBillLevel"];
        LstLocationBillLevel = (List<string>)ViewState["LstLocationBillLevel"];
        LstTaxRateBillLevel = (List<string>)ViewState["LstTaxRateBillLevel"];
        LstTaxAmtBillLevel = (List<string>)ViewState["LstTaxAmtBillLevel"];
        LstGrossAmtBillLevel = (List<string>)ViewState["LstGrossAmtBillLevel"];
        LstTaxTypeBillLevel = (List<string>)ViewState["LstTaxTypeBillLevel"];
        LstTaxAmountBillLevel = (List<string>)ViewState["LstTaxAmountBillLevel"];
        LstTotalAmountBillLevel = (List<string>)ViewState["LstTotalAmountBillLevel"];

        if (LstCountBillLevel != null)
        {
            int intCount = Convert.ToInt32(LstCountBillLevel.Count);

            if (hidTranFormatType.Value == "True")
            {

                trBillLevel.Visible = false;
                trItemLevel.Visible = true;
                for (int i = 0; i < intCount; i++)
                {
                    controlCounterBillLevel++;
                    HtmlTableRow HTRow = new HtmlTableRow();

                    HtmlTableCell HTCell1 = new HtmlTableCell();
                    TextBox txtItemBillLevel = new TextBox();
                    txtItemBillLevel.ID = LstItemBillLevel[i].ToString();
                    txtItemBillLevel.CssClass = "input";
                    txtItemBillLevel.Width = Unit.Percentage(98);
                    HiddenField hidItemIdBillLevel = new HiddenField();
                    hidItemIdBillLevel.ID = LstItemIdBillLevel[i].ToString();
                    HTCell1.Controls.Add(txtItemBillLevel);
                    HTCell1.Controls.Add(hidItemIdBillLevel);

                    HtmlTableCell HTCell2 = new HtmlTableCell();
                    TextBox txtUnitBillLevel = new TextBox();
                    txtUnitBillLevel.ID = LstUnitBillLevel[i].ToString();
                    txtUnitBillLevel.CssClass = "input";
                    txtUnitBillLevel.Width = Unit.Percentage(98);
                    HiddenField hidUnitIdBillLevel = new HiddenField();
                    hidUnitIdBillLevel.ID = LstUnitIdBillLevel[i].ToString();
                    HTCell2.Controls.Add(txtUnitBillLevel);
                    HTCell2.Controls.Add(hidUnitIdBillLevel);


                    HtmlTableCell HTCell3 = new HtmlTableCell();
                    TextBox txtQuantityBillLevel = new TextBox();
                    txtQuantityBillLevel.ID = LstQtyBillLevel[i].ToString();
                    txtQuantityBillLevel.CssClass = "inputnumber";
                    txtQuantityBillLevel.Width = Unit.Percentage(98);
                    HTCell3.Controls.Add(txtQuantityBillLevel);

                    HtmlTableCell HTCell4 = new HtmlTableCell();
                    Button btnQtyBillLevel = new Button();
                    btnQtyBillLevel.ID = LstButtonBillLevel[i].ToString();
                    btnQtyBillLevel.Text = "...";
                    btnQtyBillLevel.CssClass = "button";
                    btnQtyBillLevel.Width = Unit.Percentage(98);
                    HiddenField hidLocationBillLevel = new HiddenField();
                    hidLocationBillLevel.ID = LstLocationBillLevel[i].ToString();
                    HTCell4.Controls.Add(btnQtyBillLevel);
                    HTCell4.Controls.Add(hidLocationBillLevel);

                    HtmlTableCell HTCell5 = new HtmlTableCell();
                    TextBox txtRateBillLevel = new TextBox();
                    txtRateBillLevel.ID = LstRateBillLevel[i].ToString();
                    txtRateBillLevel.CssClass = "inputnumber";
                    txtRateBillLevel.Width = Unit.Percentage(98);
                    HiddenField hidTaxRateBillLevel = new HiddenField();
                    hidTaxRateBillLevel.ID = LstTaxRateBillLevel[i].ToString();
                    HiddenField hidTaxAmtBillLevel = new HiddenField();
                    hidTaxAmtBillLevel.ID = LstTaxAmtBillLevel[i].ToString();
                    HTCell5.Controls.Add(txtRateBillLevel);
                    HTCell5.Controls.Add(hidTaxRateBillLevel);
                    HTCell5.Controls.Add(hidTaxAmtBillLevel);


                    HtmlTableCell HTCell6 = new HtmlTableCell();
                    TextBox txtGrossAmtBillLevel = new TextBox();
                    txtGrossAmtBillLevel.ID = LstGrossAmtBillLevel[i].ToString();
                    txtGrossAmtBillLevel.CssClass = "inputnumber";
                    txtGrossAmtBillLevel.Width = Unit.Percentage(98);
                    HTCell6.Controls.Add(txtGrossAmtBillLevel);



                    HtmlTableCell HTCell7 = new HtmlTableCell();
                    DropDownList ddlTaxType = new DropDownList();
                    ddlTaxType.ID = LstTaxTypeBillLevel[i].ToString();
                    ddlTaxType.CssClass = "dropdown";
                    ddlTaxType.Width = Unit.Percentage(98);
                    HTCell7.Controls.Add(ddlTaxType);


                    HtmlTableCell HTCell8 = new HtmlTableCell();
                    TextBox txtTaxAmountBillLevel = new TextBox();
                    txtTaxAmountBillLevel.ID = LstTaxAmountBillLevel[i].ToString();
                    txtTaxAmountBillLevel.CssClass = "inputnumber";
                    txtTaxAmountBillLevel.Width = Unit.Percentage(98);
                    HTCell8.Controls.Add(txtTaxAmountBillLevel);


                    HtmlTableCell HTCell9 = new HtmlTableCell();
                    TextBox txtTotalAmountBillLevel = new TextBox();
                    txtTotalAmountBillLevel.ID = LstTotalAmountBillLevel[i].ToString();
                    txtTotalAmountBillLevel.CssClass = "inputnumber";
                    txtTotalAmountBillLevel.Width = Unit.Percentage(98);
                    HTCell9.Controls.Add(txtTotalAmountBillLevel);



                    HTRow.Cells.Add(HTCell1);
                    HTRow.Cells.Add(HTCell2);
                    HTRow.Cells.Add(HTCell3);
                    HTRow.Cells.Add(HTCell4);
                    HTRow.Cells.Add(HTCell5);
                    HTRow.Cells.Add(HTCell6);
                    HTRow.Cells.Add(HTCell7);
                    HTRow.Cells.Add(HTCell8);
                    HTRow.Cells.Add(HTCell9);

                    tblBillLevel.Rows.Add(HTRow);

                }
            }
            else if (hidTranFormatType.Value == "False")
            {
                trBillLevel.Visible = true;
                trItemLevel.Visible = false;
                for (int i = 0; i < intCount; i++)
                {
                    controlCounterBillLevel++;
                    HtmlTableRow HTRow = new HtmlTableRow();

                    HtmlTableCell HTCell1 = new HtmlTableCell();
                    TextBox txtItemBillLevel = new TextBox();
                    txtItemBillLevel.ID = LstItemBillLevel[i].ToString();
                    txtItemBillLevel.CssClass = "input";
                    txtItemBillLevel.Width = Unit.Percentage(98);
                    HiddenField hidItemIdBillLevel = new HiddenField();
                    hidItemIdBillLevel.ID = LstItemIdBillLevel[i].ToString();
                    HTCell1.Controls.Add(txtItemBillLevel);
                    HTCell1.Controls.Add(hidItemIdBillLevel);

                    HtmlTableCell HTCell2 = new HtmlTableCell();
                    TextBox txtUnitBillLevel = new TextBox();
                    txtUnitBillLevel.ID = LstUnitBillLevel[i].ToString();
                    txtUnitBillLevel.CssClass = "input";
                    txtUnitBillLevel.Width = Unit.Percentage(98);
                    HiddenField hidUnitIdBillLevel = new HiddenField();
                    hidUnitIdBillLevel.ID = LstUnitIdBillLevel[i].ToString();
                    HTCell2.Controls.Add(txtUnitBillLevel);
                    HTCell2.Controls.Add(hidUnitIdBillLevel);


                    HtmlTableCell HTCell3 = new HtmlTableCell();
                    TextBox txtQuantityBillLevel = new TextBox();
                    txtQuantityBillLevel.ID = LstQtyBillLevel[i].ToString();
                    txtQuantityBillLevel.CssClass = "inputnumber";
                    txtQuantityBillLevel.Width = Unit.Percentage(98);
                    HTCell3.Controls.Add(txtQuantityBillLevel);

                    HtmlTableCell HTCell4 = new HtmlTableCell();
                    Button btnQtyBillLevel = new Button();
                    btnQtyBillLevel.ID = LstButtonBillLevel[i].ToString();
                    btnQtyBillLevel.Text = "...";
                    btnQtyBillLevel.CssClass = "button";
                    btnQtyBillLevel.Width = Unit.Percentage(98);
                    HiddenField hidLocationBillLevel = new HiddenField();
                    hidLocationBillLevel.ID = LstLocationBillLevel[i].ToString();
                    HTCell4.Controls.Add(btnQtyBillLevel);
                    HTCell4.Controls.Add(hidLocationBillLevel);

                    HtmlTableCell HTCell5 = new HtmlTableCell();
                    TextBox txtRateBillLevel = new TextBox();
                    txtRateBillLevel.ID = LstRateBillLevel[i].ToString();
                    txtRateBillLevel.CssClass = "inputnumber";
                    txtRateBillLevel.Width = Unit.Percentage(98);
                    HiddenField hidTaxRateBillLevel = new HiddenField();
                    hidTaxRateBillLevel.ID = LstTaxRateBillLevel[i].ToString();
                    HiddenField hidTaxAmtBillLevel = new HiddenField();
                    hidTaxAmtBillLevel.ID = LstTaxAmtBillLevel[i].ToString();
                    HTCell5.Controls.Add(txtRateBillLevel);
                    HTCell5.Controls.Add(hidTaxRateBillLevel);
                    HTCell5.Controls.Add(hidTaxAmtBillLevel);

                    HtmlTableCell HTCell6 = new HtmlTableCell();
                    TextBox txtTotalAmountBillLevel = new TextBox();
                    txtTotalAmountBillLevel.ID = LstTotalAmountBillLevel[i].ToString();
                    txtTotalAmountBillLevel.CssClass = "inputnumber";
                    txtTotalAmountBillLevel.Width = Unit.Percentage(98);
                    HTCell6.Controls.Add(txtTotalAmountBillLevel);


                    HTRow.Cells.Add(HTCell1);
                    HTRow.Cells.Add(HTCell2);
                    HTRow.Cells.Add(HTCell3);
                    HTRow.Cells.Add(HTCell4);
                    HTRow.Cells.Add(HTCell5);
                    HTRow.Cells.Add(HTCell6);

                    tblBillLevel.Rows.Add(HTRow);

                }
            }
        }
    }


    protected void CheckFormatAndCustomerIsNull()//check Transaction format and Customer is Null Or Not
    {
        // if dropdownlist have not any data then it display Message.
        if (ddlFormat.Items.Count == 0)
        {
            modalFormatExtender.Show();
        }
        else if (ddlSupplier.Items.Count == 0)
        {
            modalCustExtender.Show();
        }
    }

    protected void ddlSupplier_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        Session["DTPurchaseReturn"] = null;
        hidCountReturnRow.Value = "0";
        BindSaleReturnItems();
    }

    protected void ddlVatBillLevel_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        string strTaxRate = "0";
        if (ddlVatBillLevel.SelectedValue != "0")
        {
            SqlParameter[] sparam =
                                    {
                                        new SqlParameter("@MAINCLASSID",ddlTaxGroup.SelectedValue),
                                        new SqlParameter("@SUBCLASSID",ddlVatBillLevel.SelectedValue)
                                    };
            DataTable DTTaxRate = DU.getdatausingprocedure("USP_IVR_GetTaxRate", sparam);
            if (DTTaxRate.Rows.Count > 0)
            {
                if (DTTaxRate.Rows[0]["TaxRate"].ToString() != string.Empty)
                {
                    strTaxRate = DTTaxRate.Rows[0]["TaxRate"].ToString();
                }
            }
        }
        int rowCount = Convert.ToInt32(hidControlBillLevel.Value);
        for (int i = 1; i <= rowCount; i++)
        {
            HiddenField hidTaxRateBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidTaxRateBillLevel" + i.ToString());
            hidTaxRateBillLevel.Value = strTaxRate.ToString();
        }
        hidTaxRateBillLevelMain.Value = strTaxRate.ToString();
        txtVatBillLevel.Text = strTaxRate.ToString();
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Script", "CalculateAmtBillLevel();", true);
    }

    protected void ddlTaxTypeBillLevel_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddlTaxType = (DropDownList)sender;
        string ddlId = ddlTaxType.ID.ToString();
        int intRowNo = Convert.ToInt32(ddlId.ToString().Substring(10));
        HiddenField hidTaxRateBillLevel = (HiddenField)tblBillLevel.Rows[intRowNo].FindControl("hidTaxRateBillLevel" + intRowNo.ToString());
        if (ddlTaxType.SelectedValue != "0")
        {
            SqlParameter[] sparam =
                                    {
                                        new SqlParameter("@MAINCLASSID",ddlTaxGroup.SelectedValue),
                                        new SqlParameter("@SUBCLASSID",ddlTaxType.SelectedValue)
                                    };
            DataTable DTTaxRate = DU.getdatausingprocedure("USP_IVR_GetTaxRate", sparam);
            if (DTTaxRate.Rows.Count > 0)
            {
                if (DTTaxRate.Rows[0]["TaxRate"].ToString() != string.Empty)
                {
                    hidTaxRateBillLevel.Value = DTTaxRate.Rows[0]["TaxRate"].ToString();
                }
                else
                {
                    hidTaxRateBillLevel.Value = "0";
                }
            }
        }
        else
        {
            hidTaxRateBillLevel.Value = "0";
        }
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Script", "CalculateAmtBillLevel();", true);
    }

    protected void BindJavaScriptBillLevel()//A common function for add javascript attribute to btn quantity
    {
        int intCount = Convert.ToInt32(hidControlBillLevel.Value);
        for (int i = 1; i <= intCount; i++)
        {
            HiddenField hidItemIdBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidItemIdBillLevel" + i.ToString());
            TextBox txtQuantityBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtQuantityBillLevel" + i.ToString());
            HiddenField hidLocationBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidLocationBillLevel" + i.ToString());
            Button btnQtyBillLevel = (Button)tblBillLevel.Rows[i].FindControl("btnQtyBillLevel" + i.ToString());
            btnQtyBillLevel.Attributes.Add("onclick", "return GetQuantity('" + hidItemIdBillLevel.Value + "', '" + txtQuantityBillLevel.ClientID.ToString() + "', '" + hidLocationBillLevel.ClientID.ToString() + "');");
            
        }
    }

    protected void BindSaleReturnItems()// this function bind data to table and this data somes from popup window.
    {
        if (Session["DTPurchaseReturn"] != null)
        {
            DataTable DTBind = (DataTable)Session["DTPurchaseReturn"];
            if (DTBind.Rows.Count > 0)
            {
                int CountHidReturn = Convert.ToInt32(hidCountReturnRow.Value);
                int tableCount = Convert.ToInt32(DTBind.Rows.Count);

                if (CountHidReturn == 0)
                {
                    controlCounterBillLevel = 0;
                    for (int i = 0; i < tableCount; i++)
                    {
                        AddMoreBillLevel();
                    }
                    for (int i = 1; i <= tableCount; i++)
                    {

                        TextBox txtItemBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtItemBillLevel" + i.ToString());
                        TextBox txtUnitBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtUnitBillLevel" + i.ToString());
                        HiddenField hidItemIdBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidItemIdBillLevel" + i.ToString());
                        HiddenField hidUnitIdBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidUnitIdBillLevel" + i.ToString());

                        txtItemBillLevel.Text = DTBind.Rows[i - 1]["ItemName"].ToString();
                        txtUnitBillLevel.Text = DTBind.Rows[i - 1]["UomSymbol"].ToString();
                        hidItemIdBillLevel.Value = DTBind.Rows[i - 1]["ItemId"].ToString();
                        hidUnitIdBillLevel.Value = DTBind.Rows[i - 1]["UOMID"].ToString();
                    }
                    hidCountReturnRow.Value = tableCount.ToString();
                }
                else if (CountHidReturn > tableCount)
                {
                    for (int i = CountHidReturn; i > tableCount; i--)
                    {
                        if (hidTranFormatType.Value.ToString() == "True")
                        {
                            TextBox txtItemBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtItemBillLevel" + i.ToString());
                            HiddenField hidItemIdBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidItemIdBillLevel" + i.ToString());

                            TextBox txtUnitBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtUnitBillLevel" + i.ToString());
                            HiddenField hidUnitIdBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidUnitIdBillLevel" + i.ToString());

                            TextBox txtQuantityBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtQuantityBillLevel" + i.ToString());

                            Button btnQtyBillLevel = (Button)tblBillLevel.Rows[i].FindControl("btnQtyBillLevel" + i.ToString());
                            HiddenField hidLocationBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidLocationBillLevel" + i.ToString());

                            TextBox txtRateBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtRateBillLevel" + i.ToString());
                            HiddenField hidTaxRateBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidTaxRateBillLevel" + i.ToString());
                            HiddenField hidTaxAmtBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidTaxAmtBillLevel" + i.ToString());

                            TextBox txtGrossAmtBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtGrossAmtBillLevel" + i.ToString());
                            DropDownList ddlTaxType = (DropDownList)tblBillLevel.Rows[i].FindControl("ddlTaxType" + i.ToString());
                            TextBox txtTaxAmountBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtTaxAmountBillLevel" + i.ToString());

                            DropDownList ddlSaleLedger = (DropDownList)tblBillLevel.Rows[i].FindControl("ddlSaleLedger" + i.ToString());
                            DropDownList ddlSaleTaxLedger = (DropDownList)tblBillLevel.Rows[i].FindControl("ddlSaleTaxLedger" + i.ToString());


                            TextBox txtTotalAmountBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtTotalAmountBillLevel" + i.ToString());

                            tblBillLevel.Rows[i].Cells[0].Controls.Remove(txtItemBillLevel);
                            tblBillLevel.Rows[i].Cells[0].Controls.Remove(hidUnitIdBillLevel);

                            tblBillLevel.Rows[i].Cells[1].Controls.Remove(txtUnitBillLevel);
                            tblBillLevel.Rows[i].Cells[1].Controls.Remove(hidUnitIdBillLevel);

                            tblBillLevel.Rows[i].Cells[2].Controls.Remove(txtQuantityBillLevel);

                            tblBillLevel.Rows[i].Cells[3].Controls.Remove(btnQtyBillLevel);
                            tblBillLevel.Rows[i].Cells[3].Controls.Remove(hidLocationBillLevel);

                            tblBillLevel.Rows[i].Cells[4].Controls.Remove(txtRateBillLevel);
                            tblBillLevel.Rows[i].Cells[4].Controls.Remove(hidTaxRateBillLevel);
                            tblBillLevel.Rows[i].Cells[4].Controls.Remove(hidTaxAmtBillLevel);

                            tblBillLevel.Rows[i].Cells[5].Controls.Remove(txtGrossAmtBillLevel);

                            tblBillLevel.Rows[i].Cells[6].Controls.Remove(ddlTaxType);

                            tblBillLevel.Rows[i].Cells[7].Controls.Remove(txtTaxAmountBillLevel);

                            tblBillLevel.Rows[i].Cells[8].Controls.Remove(txtTotalAmountBillLevel);

                            tblBillLevel.Rows[i].Cells[9].Controls.Remove(ddlSaleLedger);
                            tblBillLevel.Rows[i].Cells[10].Controls.Remove(ddlSaleTaxLedger);

                        }
                        else if (hidTranFormatType.Value.ToString() == "False")
                        {
                            TextBox txtItemBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtItemBillLevel" + i.ToString());
                            HiddenField hidItemIdBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidItemIdBillLevel" + i.ToString());

                            TextBox txtUnitBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtUnitBillLevel" + i.ToString());
                            HiddenField hidUnitIdBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidUnitIdBillLevel" + i.ToString());

                            TextBox txtQuantityBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtQuantityBillLevel" + i.ToString());

                            Button btnQtyBillLevel = (Button)tblBillLevel.Rows[i].FindControl("btnQtyBillLevel" + i.ToString());
                            HiddenField hidLocationBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidLocationBillLevel" + i.ToString());

                            TextBox txtRateBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtRateBillLevel" + i.ToString());
                            HiddenField hidTaxRateBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidTaxRateBillLevel" + i.ToString());
                            HiddenField hidTaxAmtBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidTaxAmtBillLevel" + i.ToString());

                            TextBox txtTotalAmountBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtTotalAmountBillLevel" + i.ToString());

                            tblBillLevel.Rows[i].Cells[0].Controls.Remove(txtItemBillLevel);
                            tblBillLevel.Rows[i].Cells[0].Controls.Remove(hidUnitIdBillLevel);

                            tblBillLevel.Rows[i].Cells[1].Controls.Remove(txtUnitBillLevel);
                            tblBillLevel.Rows[i].Cells[1].Controls.Remove(hidUnitIdBillLevel);

                            tblBillLevel.Rows[i].Cells[2].Controls.Remove(txtQuantityBillLevel);

                            tblBillLevel.Rows[i].Cells[3].Controls.Remove(btnQtyBillLevel);
                            tblBillLevel.Rows[i].Cells[3].Controls.Remove(hidLocationBillLevel);

                            tblBillLevel.Rows[i].Cells[4].Controls.Remove(txtRateBillLevel);
                            tblBillLevel.Rows[i].Cells[4].Controls.Remove(hidTaxRateBillLevel);
                            tblBillLevel.Rows[i].Cells[4].Controls.Remove(hidTaxAmtBillLevel);

                            tblBillLevel.Rows[i].Cells[5].Controls.Remove(txtTotalAmountBillLevel);
                        }
                        tblBillLevel.Rows.RemoveAt(i);
                    }
                    for (int i = 1; i <= tableCount; i++)
                    {
                        TextBox txtItemBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtItemBillLevel" + i.ToString());
                        TextBox txtUnitBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtUnitBillLevel" + i.ToString());
                        HiddenField hidItemIdBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidItemIdBillLevel" + i.ToString());
                        HiddenField hidUnitIdBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidUnitIdBillLevel" + i.ToString());

                        txtItemBillLevel.Text = DTBind.Rows[i - 1]["ItemName"].ToString();
                        txtUnitBillLevel.Text = DTBind.Rows[i - 1]["UomSymbol"].ToString();
                        hidItemIdBillLevel.Value = DTBind.Rows[i - 1]["ItemId"].ToString();
                        hidUnitIdBillLevel.Value = DTBind.Rows[i - 1]["UOMID"].ToString();
                    }
                    hidCountReturnRow.Value = tableCount.ToString();
                    hidControlBillLevel.Value = tableCount.ToString();
                }
                else if (CountHidReturn < tableCount)
                {
                    controlCounterBillLevel = CountHidReturn;
                    for (int i = CountHidReturn; i < tableCount; i++)
                    {
                        AddMoreBillLevel();
                    }
                    for (int i = 1; i <= tableCount; i++)
                    {

                        TextBox txtItemBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtItemBillLevel" + i.ToString());
                        TextBox txtUnitBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtUnitBillLevel" + i.ToString());
                        HiddenField hidItemIdBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidItemIdBillLevel" + i.ToString());
                        HiddenField hidUnitIdBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidUnitIdBillLevel" + i.ToString());

                        txtItemBillLevel.Text = DTBind.Rows[i - 1]["ItemName"].ToString();
                        txtUnitBillLevel.Text = DTBind.Rows[i - 1]["UomSymbol"].ToString();
                        hidItemIdBillLevel.Value = DTBind.Rows[i - 1]["ItemId"].ToString();
                        hidUnitIdBillLevel.Value = DTBind.Rows[i - 1]["UOMID"].ToString();
                    }
                    hidCountReturnRow.Value = tableCount.ToString();
                }
                else if (CountHidReturn == tableCount)
                {
                    //for (int i = 1; i <= tableCount; i++)
                    //{
                    //    TextBox txtItemBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtItemBillLevel" + i.ToString());
                    //    TextBox txtUnitBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtUnitBillLevel" + i.ToString());
                    //    HiddenField hidItemIdBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidItemIdBillLevel" + i.ToString());
                    //    HiddenField hidUnitIdBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidUnitIdBillLevel" + i.ToString());

                    //    txtItemBillLevel.Text = DTBind.Rows[i - 1]["ItemName"].ToString();
                    //    txtUnitBillLevel.Text = DTBind.Rows[i - 1]["UomSymbol"].ToString();
                    //    hidItemIdBillLevel.Value = DTBind.Rows[i - 1]["ItemId"].ToString();
                    //    hidUnitIdBillLevel.Value = DTBind.Rows[i - 1]["UOMID"].ToString();
                    //}
                    hidCountReturnRow.Value = tableCount.ToString();
                }
                BindDdlTaxType();
            }
        }
        else if (Session["DTPurchaseReturn"] == null)
        {
            if (tblBillLevel.Rows.Count > 1)
            {
                for (int i = tblBillLevel.Rows.Count - 1; i > 0; i--)
                {
                    if (hidTranFormatType.Value.ToString() == "True")
                    {
                        TextBox txtItemBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtItemBillLevel" + i.ToString());
                        HiddenField hidItemIdBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidItemIdBillLevel" + i.ToString());

                        TextBox txtUnitBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtUnitBillLevel" + i.ToString());
                        HiddenField hidUnitIdBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidUnitIdBillLevel" + i.ToString());

                        TextBox txtQuantityBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtQuantityBillLevel" + i.ToString());

                        Button btnQtyBillLevel = (Button)tblBillLevel.Rows[i].FindControl("btnQtyBillLevel" + i.ToString());
                        HiddenField hidLocationBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidLocationBillLevel" + i.ToString());

                        TextBox txtRateBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtRateBillLevel" + i.ToString());
                        HiddenField hidTaxRateBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidTaxRateBillLevel" + i.ToString());
                        HiddenField hidTaxAmtBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidTaxAmtBillLevel" + i.ToString());

                        TextBox txtGrossAmtBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtGrossAmtBillLevel" + i.ToString());
                        DropDownList ddlTaxType = (DropDownList)tblBillLevel.Rows[i].FindControl("ddlTaxType" + i.ToString());
                        TextBox txtTaxAmountBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtTaxAmountBillLevel" + i.ToString());

                        DropDownList ddlSaleLedger = (DropDownList)tblBillLevel.Rows[i].FindControl("ddlSaleLedger" + i.ToString());
                        DropDownList ddlSaleTaxLedger = (DropDownList)tblBillLevel.Rows[i].FindControl("ddlSaleTaxLedger" + i.ToString());


                        TextBox txtTotalAmountBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtTotalAmountBillLevel" + i.ToString());

                        tblBillLevel.Rows[i].Cells[0].Controls.Remove(txtItemBillLevel);
                        tblBillLevel.Rows[i].Cells[0].Controls.Remove(hidUnitIdBillLevel);

                        tblBillLevel.Rows[i].Cells[1].Controls.Remove(txtUnitBillLevel);
                        tblBillLevel.Rows[i].Cells[1].Controls.Remove(hidUnitIdBillLevel);

                        tblBillLevel.Rows[i].Cells[2].Controls.Remove(txtQuantityBillLevel);

                        tblBillLevel.Rows[i].Cells[3].Controls.Remove(btnQtyBillLevel);
                        tblBillLevel.Rows[i].Cells[3].Controls.Remove(hidLocationBillLevel);

                        tblBillLevel.Rows[i].Cells[4].Controls.Remove(txtRateBillLevel);
                        tblBillLevel.Rows[i].Cells[4].Controls.Remove(hidTaxRateBillLevel);
                        tblBillLevel.Rows[i].Cells[4].Controls.Remove(hidTaxAmtBillLevel);

                        tblBillLevel.Rows[i].Cells[5].Controls.Remove(txtGrossAmtBillLevel);

                        tblBillLevel.Rows[i].Cells[6].Controls.Remove(ddlTaxType);

                        tblBillLevel.Rows[i].Cells[7].Controls.Remove(txtTaxAmountBillLevel);

                        tblBillLevel.Rows[i].Cells[8].Controls.Remove(txtTotalAmountBillLevel);

                        tblBillLevel.Rows[i].Cells[9].Controls.Remove(ddlSaleLedger);
                        tblBillLevel.Rows[i].Cells[10].Controls.Remove(ddlSaleTaxLedger);


                    }
                    else if (hidTranFormatType.Value.ToString() == "False")
                    {
                        TextBox txtItemBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtItemBillLevel" + i.ToString());
                        HiddenField hidItemIdBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidItemIdBillLevel" + i.ToString());

                        TextBox txtUnitBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtUnitBillLevel" + i.ToString());
                        HiddenField hidUnitIdBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidUnitIdBillLevel" + i.ToString());

                        TextBox txtQuantityBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtQuantityBillLevel" + i.ToString());

                        Button btnQtyBillLevel = (Button)tblBillLevel.Rows[i].FindControl("btnQtyBillLevel" + i.ToString());
                        HiddenField hidLocationBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidLocationBillLevel" + i.ToString());

                        TextBox txtRateBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtRateBillLevel" + i.ToString());
                        HiddenField hidTaxRateBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidTaxRateBillLevel" + i.ToString());
                        HiddenField hidTaxAmtBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidTaxAmtBillLevel" + i.ToString());

                        TextBox txtTotalAmountBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtTotalAmountBillLevel" + i.ToString());

                        tblBillLevel.Rows[i].Cells[0].Controls.Remove(txtItemBillLevel);
                        tblBillLevel.Rows[i].Cells[0].Controls.Remove(hidUnitIdBillLevel);

                        tblBillLevel.Rows[i].Cells[1].Controls.Remove(txtUnitBillLevel);
                        tblBillLevel.Rows[i].Cells[1].Controls.Remove(hidUnitIdBillLevel);

                        tblBillLevel.Rows[i].Cells[2].Controls.Remove(txtQuantityBillLevel);

                        tblBillLevel.Rows[i].Cells[3].Controls.Remove(btnQtyBillLevel);
                        tblBillLevel.Rows[i].Cells[3].Controls.Remove(hidLocationBillLevel);

                        tblBillLevel.Rows[i].Cells[4].Controls.Remove(txtRateBillLevel);
                        tblBillLevel.Rows[i].Cells[4].Controls.Remove(hidTaxRateBillLevel);
                        tblBillLevel.Rows[i].Cells[4].Controls.Remove(hidTaxAmtBillLevel);

                        tblBillLevel.Rows[i].Cells[5].Controls.Remove(txtTotalAmountBillLevel);
                    }
                    tblBillLevel.Rows.RemoveAt(i);
                }
                controlCounterBillLevel = 0;
                hidControlBillLevel.Value = "0";
            }
        }
    }

    protected bool isBatchEnabled(string ItemId)//A common function for check item batch enabled or not
    {
        bool Flag = true;
        if (ItemId.ToString() != string.Empty)
        {
            strsql = "Select ITMBATCH from IVR_ITEM Where ITMID='" + ItemId.ToString() + "'";
            DataTable DT = DU.getdata(strsql);
            if (DT.Rows.Count > 0)
            {
                if (Convert.ToDouble(DT.Rows[0]["ITMBATCH"]) > 0)
                {
                    Flag = true;
                }
                else
                {
                    Flag = false;
                }
            }
        }
        return Flag;
    }

    protected bool isWarrantyEnabled(string ItemId)//A common function for check item warranty enabled or not
    {
        bool Flag = true;
        if (ItemId.ToString() != string.Empty)
        {
            strsql = "Select ITMBATCH, ITMBATCHOPT from IVR_ITEM Where ITMID='" + ItemId.ToString() + "'";
            DataTable DT = DU.getdata(strsql);
            if (DT.Rows.Count > 0)
            {
                if (Convert.ToDouble(DT.Rows[0]["ITMBATCH"]) > 0 && Convert.ToDouble(DT.Rows[0]["ITMBATCHOPT"]) == 1)
                {
                    Flag = true;
                }
                else
                {
                    Flag = false;
                }
            }
        }
        return Flag;
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        InsertItemTranMainBillLevel(InsertVouchersBillLevel());//Save all information into database
    }

    protected string InsertVouchersBillLevel()// Insert records in ACC_Voucher Table and return Last Inserted  ID From database 
    {
        string strRefId = string.Empty;
        string strFinYear = string.Empty;
        string strTransactionTypeID = string.Empty;
        string strTransDate = string.Empty;
        int intResult = 0;
        try
        {
            SqlParameter[] sparam = {
                                        new SqlParameter("@PARENTTYPEID","8"),
                                        new SqlParameter("@BILLFORMATID",ddlFormat.SelectedValue),
                                        new SqlParameter("@MODULETYPE","2")
                                    };
            DataTable DTRef = DU.getdatausingprocedure("USP_ACC_GetTransactionSettingBasedOnFormat", sparam);
            if (DTRef.Rows.Count > 0)
            {
                strTransactionTypeID = DTRef.Rows[0]["TYPEID"].ToString();
            }
            if (Session["FINYEAR"] != null)
            {
                strFinYear = Session["FINYEAR"].ToString();
            }
            if (txtDate.Text.ToString() != string.Empty)
            {
                string[] str = txtDate.Text.ToString().Split('/');
                strTransDate = str[1].ToString() + "/" + str[0].ToString() + "/" + str[2].ToString();
            }
            if (strTransactionTypeID.ToString() != string.Empty)
            {
                SqlParameter[] sparam1 = { 
                                    new SqlParameter("@TransactionTypeID",strTransactionTypeID.ToString()),
                                    new SqlParameter("@TransactionNumber",txtVoucher.Text.Trim()),
                                    new SqlParameter("@FinYear",strFinYear.ToString()),
                                    new SqlParameter("@ReferenceNumber",txtBillRef1.Text.Trim()),
                                    new SqlParameter("@IsCashVoucher","0"),
                                    new SqlParameter("@TransactionDate",strTransDate.ToString()),
                                    new SqlParameter("@CreditDays",txtCreditDay.Text.Trim()),
                                    new SqlParameter("@CreatedBy",Session["username"].ToString()),
                                    new SqlParameter("@IsCancelled","0"),
                                    new SqlParameter("@CostCentreClassID","0")
                                };
                intResult = DU.executeprocedure("USP_ACC_SaveTransaction", sparam1);
                if (intResult > 0)
                {
                    strsql = "Select Ident_Current('ACC_VOUCHERS') as TranId";
                    DataTable DTTranid = DU.getdata(strsql);
                    if (DTTranid.Rows.Count > 0)
                    {
                        strRefId = DTTranid.Rows[0]["TranId"].ToString();
                        InsertVoucherDetailsBillLevel(DTTranid.Rows[0]["TranId"].ToString());
                    }
                }
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message.ToString();
        }

        return strRefId.ToString();
    }

    protected void InsertVoucherDetailsBillLevel(string TranId)//Insert Records In Acc_VouDetails database Table 
    {
        if (TranId.ToString() != string.Empty)
        {
            if (ddlSupplier.SelectedValue != "0")
            {
                decimal Amount = 0;
                if (txtGrandTotalBillLevel.Text != string.Empty)
                {
                    Amount = Convert.ToDecimal(txtGrandTotalBillLevel.Text.Trim());
                }
                SqlParameter[] sparam = {
                                    new SqlParameter("@TransactionID",TranId.ToString()),
                                    new SqlParameter("@LedgerID",ddlSupplier.SelectedValue),
                                    new SqlParameter("@Amount",Amount.ToString()),
                                    new SqlParameter("@IsReconciled","0")
                                };
                DU.executeprocedure("USP_ACC_SaveTransactionDetails", sparam);
            }
            if (hidTranFormatType.Value.ToString() == "False")
            {
                if (ddlSaleLedger.SelectedValue != "0")
                {
                    decimal Amount = 0;
                    if (txtTaxableAmtBillLevel.Text != string.Empty)
                    {
                        decimal Amt = Convert.ToDecimal(txtTaxableAmtBillLevel.Text.Trim());
                        Amount = Amt;
                    }
                    SqlParameter[] sparam = {
                                    new SqlParameter("@TransactionID",TranId.ToString()),
                                    new SqlParameter("@LedgerID",ddlSaleLedger.SelectedValue),
                                    new SqlParameter("@Amount",Amount.ToString()),
                                    new SqlParameter("@IsReconciled","0")
                                };
                    DU.executeprocedure("USP_ACC_SaveTransactionDetails", sparam);
                }

                if (ddlSaleTaxLedger.SelectedValue != "0")
                {
                    decimal Amount = 0;
                    if (txtVatBillLevel.Text != string.Empty)
                    {
                        decimal Amt = Convert.ToDecimal(txtVatBillLevel.Text.Trim());
                        Amount = Amt;
                    }
                    SqlParameter[] sparam = {
                                    new SqlParameter("@TransactionID",TranId.ToString()),
                                    new SqlParameter("@LedgerID",ddlSaleTaxLedger.SelectedValue),
                                    new SqlParameter("@Amount",Amount.ToString()),
                                    new SqlParameter("@IsReconciled","0")
                                };
                    DU.executeprocedure("USP_ACC_SaveTransactionDetails", sparam);
                }
            }
            else if (hidTranFormatType.Value.ToString() == "True")
            {
                int rowCount = Convert.ToInt32(hidControlBillLevel.Value);
                for (int i = 1; i <= rowCount; i++)
                {
                    TextBox txtGrossAmtBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtGrossAmtBillLevel" + i.ToString());
                    TextBox txtTaxAmountBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtTaxAmountBillLevel" + i.ToString());
                    DropDownList ddlSaleLedger1 = (DropDownList)tblBillLevel.Rows[i].FindControl("ddlSaleLedger" + i.ToString());
                    DropDownList ddlSaleTaxLedger1 = (DropDownList)tblBillLevel.Rows[i].FindControl("ddlSaleTaxLedger" + i.ToString());

                    if (txtGrossAmtBillLevel.Text != string.Empty && Convert.ToDouble(txtGrossAmtBillLevel.Text) > 0)
                    {
                        if (ddlSaleLedger1.SelectedValue != "0")
                        {
                            decimal Amount = 0;
                            if (txtGrossAmtBillLevel.Text != string.Empty)
                            {
                                decimal Amt = Convert.ToDecimal(txtGrossAmtBillLevel.Text.Trim());
                                Amount = Amt;
                            }
                            SqlParameter[] sparam = {
                                    new SqlParameter("@TransactionID",TranId.ToString()),
                                    new SqlParameter("@LedgerID",ddlSaleLedger1.SelectedValue),
                                    new SqlParameter("@Amount",Amount.ToString()),
                                    new SqlParameter("@IsReconciled","0")
                                };
                            DU.executeprocedure("USP_ACC_SaveTransactionDetails", sparam);
                        }
                    }
                    if (txtTaxAmountBillLevel.Text != string.Empty && Convert.ToDouble(txtTaxAmountBillLevel.Text) > 0)
                    {
                        if (ddlSaleTaxLedger1.SelectedValue != "0")
                        {
                            decimal Amount = 0;
                            if (txtTaxAmountBillLevel.Text != string.Empty)
                            {
                                decimal Amt = Convert.ToDecimal(txtTaxAmountBillLevel.Text.Trim());
                                Amount = Amt;
                            }
                            SqlParameter[] sparam = {
                                    new SqlParameter("@TransactionID",TranId.ToString()),
                                    new SqlParameter("@LedgerID",ddlSaleTaxLedger1.SelectedValue),
                                    new SqlParameter("@Amount",Amount.ToString()),
                                    new SqlParameter("@IsReconciled","0")
                                };
                            DU.executeprocedure("USP_ACC_SaveTransactionDetails", sparam);
                        }
                    }
                }
            }
        }
    }

    protected void InsertItemTranMainBillLevel(string strRefId)//Insert Records In IVR_TranMain database Table
    {
        if (strRefId.ToString() != string.Empty)
        {
            string strFinYear = string.Empty;
            if (Session["FINYEAR"].ToString() != string.Empty)
            {
                strFinYear = Session["FINYEAR"].ToString();
            }
            string strTransDate = string.Empty;
            string strRefDate = "01/01/1900";
            string strEffectiveDate = string.Format("{0:MM/dd/yyyy}", System.DateTime.Now).ToString();
            if (txtDate.Text.ToString() != string.Empty)
            {
                string[] str = txtDate.Text.ToString().Split('/');
                strTransDate = str[1].ToString() + "/" + str[0].ToString() + "/" + str[2].ToString();
            }
            if (txtBillRef2.Text.ToString() != string.Empty)
            {
                string[] str = txtBillRef2.Text.ToString().Split('/');
                strRefDate = str[1].ToString() + "/" + str[0].ToString() + "/" + str[2].ToString();
            }
            if (strRefId.ToString() != string.Empty)
            {
                SqlParameter[] sparam ={
                                   new SqlParameter("@TRANREFERENCEID",strRefId.ToString()),
                                   new SqlParameter("@TRANTYPEID","6"),
                                   new SqlParameter("@FINYEAR",strFinYear.ToString()),                                  
                                   new SqlParameter("@TFOID",ddlFormat.SelectedValue),
                                   new SqlParameter("@TRNNUM",txtVoucher.Text.Trim()),
                                   new SqlParameter("@TRNDATE",strTransDate.ToString()),
                                   new SqlParameter("@VENDCUST_LID",ddlSupplier.SelectedValue),
                                   new SqlParameter("@TAX_MAINCLASSID",ddlTaxGroup.SelectedValue),
                                   new SqlParameter("@TRNSTATUS","1"),
                                   new SqlParameter("@EFFECTIVEDATE",strEffectiveDate.ToString()),
                                   new SqlParameter("@MODEOFPAY",rbtCashCredit.SelectedValue),
                                   new SqlParameter("@CREDITPERIOD",txtCreditDay.Text.Trim()),
                                   new SqlParameter("@REFNUM",txtBillRef1.Text.Trim()),
                                   new SqlParameter("@REFDATE",strRefDate.ToString()),
                                   new SqlParameter("@SURCHARGE","0"),                                   
                                   new SqlParameter("@CNAME",Session["username"].ToString())                                   
                               };
                int intResult = DU.executeprocedure("USP_IVR_SaveTranMain", sparam);
                if (intResult > 0)
                {
                    strsql = "Select Ident_Current('IVR_TRAN_MAIN') as TranId";
                    DataTable DTTranid = DU.getdata(strsql);
                    if (DTTranid.Rows.Count > 0)
                    {
                        InsertItemTranItemBillLevel(DTTranid.Rows[0]["TranId"].ToString());//Save Item Details into database table IVR_Tran_Item
                        InsertItemTranTotalBillLevel(DTTranid.Rows[0]["TranId"].ToString());//Save Transaction Total Information into Database table IVR_Tran_Total
                        InsertTranDelivery(DTTranid.Rows[0]["TranId"].ToString());//Save Delivery Address Into Databse Table IVR_Tran_Delivery
                        InsertTranOthers(DTTranid.Rows[0]["TranId"].ToString());//Save Other Tab Information into database Table IVT_Tran_Others
                    }
                    lblMsg.Text = "Purchase Return Createed..";
                }
            }
        }
    }

    protected void InsertItemTranItemBillLevel(string strTranId)//Insert Record In IVR_Tran_Item database Table 
    {
        if (strTranId.ToString() != string.Empty)
        {
            int intResult = 0;
            int intCount = Convert.ToInt32(hidControlBillLevel.Value);
            for (int i = 1; i <= intCount; i++)
            {
                if (hidTranFormatType.Value == "True")
                {
                    HiddenField hidItemIdBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidItemIdBillLevel" + i.ToString());
                    HiddenField hidUnitIdBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidUnitIdBillLevel" + i.ToString());
                    TextBox txtQuantityBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtQuantityBillLevel" + i.ToString());
                    TextBox txtRateBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtRateBillLevel" + i.ToString());
                    HiddenField hidTaxRateBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidTaxRateBillLevel" + i.ToString());
                    HiddenField hidTaxAmtBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidTaxAmtBillLevel" + i.ToString());
                    TextBox txtGrossAmtBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtGrossAmtBillLevel" + i.ToString());
                    TextBox txtTaxAmountBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtTaxAmountBillLevel" + i.ToString());
                    TextBox txtTotalAmountBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtTotalAmountBillLevel" + i.ToString());
                    DropDownList ddlSaleLedger1 = (DropDownList)tblBillLevel.Rows[i].FindControl("ddlSaleLedger" + i.ToString());
                    DropDownList ddlSaleTaxLedger1 = (DropDownList)tblBillLevel.Rows[i].FindControl("ddlSaleTaxLedger" + i.ToString());

                    if (hidTaxRateBillLevel.Value.ToString() == string.Empty)
                    {
                        hidTaxRateBillLevel.Value = "0";
                    }
                    if (hidTaxAmtBillLevel.Value.ToString() == string.Empty)
                    {
                        hidTaxAmtBillLevel.Value = "0";
                    }
                    if (txtTaxAmountBillLevel.Text == string.Empty)
                    {
                        txtTaxAmountBillLevel.Text = "0";
                    }
                    if (txtQuantityBillLevel.Text != string.Empty && txtRateBillLevel.Text != string.Empty)
                    {
                        double dblItemAmount = 0;
                        dblItemAmount = Convert.ToDouble(txtGrossAmtBillLevel.Text) + Convert.ToDouble(txtTaxAmountBillLevel.Text);

                        SqlParameter[] sparam ={
                                   new SqlParameter("@TRNID",strTranId.ToString()),
                                   new SqlParameter("@ITMID",hidItemIdBillLevel.Value),
                                   new SqlParameter("@ITMQTY",txtQuantityBillLevel.Text.Trim()),
                                   new SqlParameter("@UOMID",hidUnitIdBillLevel.Value),
                                   new SqlParameter("@RATE",txtRateBillLevel.Text.Trim()),
                                   new SqlParameter("@GROSSAMT",txtGrossAmtBillLevel.Text.Trim()),
                                   new SqlParameter("@NETAMT",dblItemAmount),
                                   new SqlParameter("@TAXPER",hidTaxRateBillLevel.Value),
                                   new SqlParameter("@TAXAMT",hidTaxAmtBillLevel.Value),
                                   new SqlParameter("@ITMAMT",dblItemAmount),
                                   new SqlParameter("@ITMAMT_LID",ddlSaleLedger1.SelectedValue),
                                   new SqlParameter("@TAXAMT_LID",ddlSaleTaxLedger1.SelectedValue)                                  
                               };
                        intResult = DU.executeprocedure("USP_IVR_SaveTranItem", sparam);
                    }
                }
                else if (hidTranFormatType.Value == "False")
                {
                    HiddenField hidItemIdBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidItemIdBillLevel" + i.ToString());
                    HiddenField hidUnitIdBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidUnitIdBillLevel" + i.ToString());
                    TextBox txtQuantityBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtQuantityBillLevel" + i.ToString());
                    TextBox txtRateBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtRateBillLevel" + i.ToString());
                    HiddenField hidTaxRateBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidTaxRateBillLevel" + i.ToString());
                    HiddenField hidTaxAmtBillLevel = (HiddenField)tblBillLevel.Rows[i].FindControl("hidTaxAmtBillLevel" + i.ToString());
                    TextBox txtTotalAmountBillLevel = (TextBox)tblBillLevel.Rows[i].FindControl("txtTotalAmountBillLevel" + i.ToString());

                    if (hidTaxRateBillLevel.Value.ToString() == string.Empty)
                    {
                        hidTaxRateBillLevel.Value = "0";
                    }
                    if (hidTaxAmtBillLevel.Value.ToString() == string.Empty)
                    {
                        hidTaxAmtBillLevel.Value = "0";
                    }
                    if (txtQuantityBillLevel.Text != string.Empty && txtRateBillLevel.Text != string.Empty)
                    {
                        double dblItemAmount = 0;
                        dblItemAmount = Convert.ToDouble(txtTotalAmountBillLevel.Text) + Convert.ToDouble(hidTaxAmtBillLevel.Value);

                        SqlParameter[] sparam ={
                                   new SqlParameter("@TRNID",strTranId.ToString()),
                                   new SqlParameter("@ITMID",hidItemIdBillLevel.Value),
                                   new SqlParameter("@ITMQTY",txtQuantityBillLevel.Text.Trim()),
                                   new SqlParameter("@UOMID",hidUnitIdBillLevel.Value.ToString()),
                                   new SqlParameter("@RATE",txtRateBillLevel.Text.Trim()),
                                   new SqlParameter("@GROSSAMT",txtTotalAmountBillLevel.Text.Trim()),
                                   new SqlParameter("@NETAMT",dblItemAmount),
                                   new SqlParameter("@TAXPER",hidTaxRateBillLevel.Value.ToString()),
                                   new SqlParameter("@TAXAMT",hidTaxAmtBillLevel.Value.ToString()),
                                   new SqlParameter("@ITMAMT",dblItemAmount)                                                                      
                               };
                        intResult = DU.executeprocedure("USP_IVR_SaveTranItem", sparam);
                    }
                }
                if (intResult > 0)
                {
                    strsql = "Select Ident_Current('IVR_TRAN_ITEM') as TITID";
                    DataTable DTTranid = DU.getdata(strsql);
                    if (DTTranid.Rows.Count > 0)
                    {
                        InsertItemTranDetailsBillLevel(strTranId, DTTranid.Rows[0]["TITID"].ToString(), i);
                        intResult = 0;
                    }
                }
            }
        }
    }

    protected void InsertItemTranDetailsBillLevel(string tranId, string tranItemId, int rowId)//Insert Item Details  into IVR_Tran_ItemDet database table
    {
        if (tranId.ToString() != string.Empty && tranItemId.ToString() != string.Empty)
        {
            string strTransDate = string.Empty;
            if (txtDate.Text.ToString() != string.Empty)
            {
                string[] str = txtDate.Text.ToString().Split('/');
                strTransDate = str[1].ToString() + "/" + str[0].ToString() + "/" + str[2].ToString();
            }
            HiddenField hidItemIdBillLevel = (HiddenField)tblBillLevel.Rows[rowId].FindControl("hidItemIdBillLevel" + rowId.ToString());
            TextBox txtRateBillLevel = (TextBox)tblBillLevel.Rows[rowId].FindControl("txtRateBillLevel" + rowId.ToString());
            HiddenField hidLocationBillLevel = (HiddenField)tblBillLevel.Rows[rowId].FindControl("hidLocationBillLevel" + rowId.ToString());
            TextBox txtQuantityBillLevel = (TextBox)tblBillLevel.Rows[rowId].FindControl("txtQuantityBillLevel" + rowId.ToString());

            if (txtQuantityBillLevel.Text != string.Empty && txtRateBillLevel.Text != string.Empty)
            {
                if (hidLocationBillLevel.Value.ToString() != string.Empty)
                {
                    if (isBatchEnabled(hidItemIdBillLevel.Value) && isWarrantyEnabled(hidItemIdBillLevel.Value))
                    {
                        List<string> lstLocationId = new List<string>();
                        List<string> lstBatchId = new List<string>();
                        List<string> lstLocationQty = new List<string>();
                        List<string> lstWarranty1 = new List<string>();
                        List<string> lstWarrantyDetails1 = new List<string>();
                        if (hidLocationBillLevel.Value.ToString().Contains("/") && hidLocationBillLevel.Value.ToString().Contains("|") && hidLocationBillLevel.Value.ToString().Contains(":"))
                        {
                            string[] strMainArray = hidLocationBillLevel.Value.ToString().Split('/');
                            foreach (string strSubArray in strMainArray)
                            {
                                string[] str = strSubArray.ToString().Split(',');
                                lstLocationId.Add(str[0].ToString());
                                string[] str1 = str[1].ToString().Split('|');
                                lstLocationQty.Add(str1[0].ToString());
                                string[] str2 = str1[1].ToString().Split(':');
                                lstBatchId.Add(str2[0].ToString());
                                string[] str3 = str2[1].ToString().Split(';');
                                lstWarranty1.Add(str3[0].ToString());
                                lstWarrantyDetails1.Add(str3[1].ToString());
                            }
                        }
                        else
                        {
                            string[] str = hidLocationBillLevel.Value.ToString().Split(',');
                            lstLocationId.Add(str[0].ToString());
                            string[] str1 = str[1].ToString().Split('|');
                            lstLocationQty.Add(str1[0].ToString());
                            string[] str2 = str1[1].ToString().Split(':');
                            lstBatchId.Add(str2[0].ToString());
                            string[] str3 = str2[1].ToString().Split(';');
                            lstWarranty1.Add(str3[0].ToString());
                            lstWarrantyDetails1.Add(str3[1].ToString());
                        }
                        if (lstLocationId != null)
                        {
                            int Count = Convert.ToInt32(lstLocationId.Count);
                            for (int j = 0; j < Count; j++)
                            {
                                SqlParameter[] sparam ={
                                                               new SqlParameter("@TITID",tranItemId.ToString()),
                                                               new SqlParameter("@WHSID",lstLocationId[j].ToString()),
                                                               new SqlParameter("@BATID",lstBatchId[j].ToString()),
                                                               new SqlParameter("@DETQTY",lstLocationQty[j].ToString()),
                                                               new SqlParameter("@WARRANTY",lstWarranty1[j].ToString()),
                                                               new SqlParameter("@WARRANTYPERIOD",lstWarrantyDetails1[j].ToString())                                                               
                                                           };
                                int intResult = DU.executeprocedure("USP_IVR_SaveTransactionItemDetails", sparam);
                                if (intResult > 0)
                                {
                                    strsql = "Select Ident_Current('IVR_TRAN_ITEMDET') as TIDID";
                                    DataTable DTTranTidid = DU.getdata(strsql);
                                    if (DTTranTidid.Rows.Count > 0)
                                    {
                                        SqlParameter[] sparam1 ={
                                                               new SqlParameter("@TYOFENTRY","6"),
                                                               new SqlParameter("@ENTRYID",tranId.ToString()),
                                                               new SqlParameter("@ITMROWID",DTTranTidid.Rows[0]["TIDID"].ToString()),
                                                               new SqlParameter("@ITMID",hidItemIdBillLevel.Value),
                                                               new SqlParameter("@WHID",lstLocationId[j].ToString()),
                                                               new SqlParameter("@BATCHID",lstBatchId[j].ToString()),                                                               
                                                               new SqlParameter("@QTYPRIM",lstLocationQty[j].ToString()),                                                              
                                                               new SqlParameter("@RATE_PRIMUNIT",txtRateBillLevel.Text.Trim()),                                                             
                                                               new SqlParameter("@ENTRYDATE",strTransDate)
                                                                };
                                        int Result = DU.executeprocedure("USP_IVR_SaveStockDetails", sparam1);
                                        if (Result > 0)
                                        {
                                            intResult = 0;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else if (isBatchEnabled(hidItemIdBillLevel.Value) && !isWarrantyEnabled(hidItemIdBillLevel.Value))
                    {
                        List<string> lstLocationId = new List<string>();
                        List<string> lstBatchId = new List<string>();
                        List<string> lstLocationQty = new List<string>();
                        if (hidLocationBillLevel.Value.ToString().Contains("/") && hidLocationBillLevel.Value.ToString().Contains("|"))
                        {
                            string[] strMainArray = hidLocationBillLevel.Value.ToString().Split('/');
                            foreach (string strSubArray in strMainArray)
                            {
                                string[] str = strSubArray.ToString().Split(',');
                                lstLocationId.Add(str[0].ToString());
                                string[] str1 = str[1].ToString().Split('|');
                                lstLocationQty.Add(str1[0].ToString());
                                lstBatchId.Add(str1[1].ToString());
                            }
                        }
                        else
                        {
                            string[] str = hidLocationBillLevel.Value.ToString().Split(',');
                            lstLocationId.Add(str[0].ToString());
                            string[] str1 = str[1].ToString().Split('|');
                            lstLocationQty.Add(str1[0].ToString());
                            lstBatchId.Add(str1[1].ToString());
                        }
                        if (lstLocationId != null)
                        {
                            int Count = Convert.ToInt32(lstLocationId.Count);
                            for (int j = 0; j < Count; j++)
                            {
                                SqlParameter[] sparam ={
                                                               new SqlParameter("@TITID",tranItemId.ToString()),
                                                               new SqlParameter("@WHSID",lstLocationId[j].ToString()),
                                                               new SqlParameter("@BATID",lstBatchId[j].ToString()),
                                                               new SqlParameter("@DETQTY",lstLocationQty[j].ToString()),
                                                               new SqlParameter("@WARRANTY","0"),
                                                               new SqlParameter("@WARRANTYPERIOD","0")                                                               
                                                           };
                                int intResult = DU.executeprocedure("USP_IVR_SaveTransactionItemDetails", sparam);
                                if (intResult > 0)
                                {
                                    strsql = "Select Ident_Current('IVR_TRAN_ITEMDET') as TIDID";
                                    DataTable DTTranTidid = DU.getdata(strsql);
                                    if (DTTranTidid.Rows.Count > 0)
                                    {
                                        SqlParameter[] sparam1 ={
                                                               new SqlParameter("@TYOFENTRY","6"),
                                                               new SqlParameter("@ENTRYID",tranId.ToString()),
                                                               new SqlParameter("@ITMROWID",DTTranTidid.Rows[0]["TIDID"].ToString()),
                                                               new SqlParameter("@ITMID",hidItemIdBillLevel.Value),
                                                               new SqlParameter("@WHID",lstLocationId[j].ToString()),
                                                               new SqlParameter("@BATCHID",lstBatchId[j].ToString()),                                                               
                                                               new SqlParameter("@QTYPRIM",lstLocationQty[j].ToString()),                                                              
                                                               new SqlParameter("@RATE_PRIMUNIT",txtRateBillLevel.Text.Trim()),                                                             
                                                               new SqlParameter("@ENTRYDATE",strTransDate)
                                                                };
                                        int Result = DU.executeprocedure("USP_IVR_SaveStockDetails", sparam1);
                                        if (Result > 0)
                                        {
                                            intResult = 0;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else if (!isBatchEnabled(hidItemIdBillLevel.Value) && !isWarrantyEnabled(hidItemIdBillLevel.Value))
                    {
                        List<string> lstLocationId = new List<string>();
                        List<string> lstLocationQty = new List<string>();
                        if (hidLocationBillLevel.Value.ToString().Contains("/"))
                        {
                            string[] strMainArray = hidLocationBillLevel.Value.ToString().Split('/');
                            foreach (string strSubArray in strMainArray)
                            {
                                string[] str = strSubArray.ToString().Split(',');
                                lstLocationId.Add(str[0].ToString());
                                lstLocationQty.Add(str[1].ToString());
                            }
                        }
                        else
                        {
                            string[] str = hidLocationBillLevel.Value.ToString().Split(',');
                            lstLocationId.Add(str[0].ToString());
                            lstLocationQty.Add(str[1].ToString());
                        }
                        if (lstLocationId != null)
                        {
                            int Count = Convert.ToInt32(lstLocationId.Count);
                            for (int j = 0; j < Count; j++)
                            {
                                SqlParameter[] sparam ={
                                                               new SqlParameter("@TITID",tranItemId.ToString()),
                                                               new SqlParameter("@WHSID",lstLocationId[j].ToString()),                                                               
                                                               new SqlParameter("@DETQTY",lstLocationQty[j].ToString()),
                                                               new SqlParameter("@WARRANTY","0"),
                                                               new SqlParameter("@WARRANTYPERIOD","0")                                                               
                                                           };
                                int intResult = DU.executeprocedure("USP_IVR_SaveTransactionItemDetails", sparam);
                                if (intResult > 0)
                                {
                                    strsql = "Select Ident_Current('IVR_TRAN_ITEMDET') as TIDID";
                                    DataTable DTTranTidid = DU.getdata(strsql);
                                    if (DTTranTidid.Rows.Count > 0)
                                    {
                                        SqlParameter[] sparam1 ={
                                                               new SqlParameter("@TYOFENTRY","6"),
                                                               new SqlParameter("@ENTRYID",tranId.ToString()),
                                                               new SqlParameter("@ITMROWID",DTTranTidid.Rows[0]["TIDID"].ToString()),
                                                               new SqlParameter("@ITMID",hidItemIdBillLevel.Value),
                                                               new SqlParameter("@WHID",lstLocationId[j].ToString()),
                                                               new SqlParameter("@BATCHID","0"),                                                               
                                                               new SqlParameter("@QTYPRIM",lstLocationQty[j].ToString()),                                                              
                                                               new SqlParameter("@RATE_PRIMUNIT",txtRateBillLevel.Text.Trim()),                                                             
                                                               new SqlParameter("@ENTRYDATE",strTransDate)
                                                                };
                                        int Result = DU.executeprocedure("USP_IVR_SaveStockDetails", sparam1);
                                        if (Result > 0)
                                        {
                                            intResult = 0;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    protected void InsertItemTranTotalBillLevel(string strTranId)//Insert Transaction Total Information Into IVR_Tran_Total Database table
    {
        if (strTranId.ToString() != string.Empty)
        {
            if (hidTranFormatType.Value == "False")
            {
                int intResult = 0;
                SqlParameter[] sparam ={
                           new SqlParameter("@TRNID",strTranId.ToString()),
                           new SqlParameter("@ITEMTOTAL",txtTotalItemBillLevel.Text.Trim()),
                           new SqlParameter("@ITEMTOTAL_LID",ddlSaleLedger.SelectedValue),
                           new SqlParameter("@TAXABLEAMT",txtTaxableAmtBillLevel.Text.Trim()),
                           new SqlParameter("@TAX_TXMASID",ddlVatBillLevel.SelectedValue),
                           new SqlParameter("@TAXPER",hidTaxRateBillLevelMain.Value.ToString()),
                           new SqlParameter("@TAXAMT",txtVatBillLevel.Text.Trim()),
                           new SqlParameter("@TAXAMT_LID",ddlSaleTaxLedger.SelectedValue),
                           new SqlParameter("@RNDOFFAMT",txtRoundOffBillLevel.Text.Trim()),
                           //new SqlParameter("@RNDOFFAMT_LID",ddlRoundOff.SelectedValue),
                           new SqlParameter("@TRNTOTAL",txtGrandTotalBillLevel.Text.Trim())                                                                                                                 
                       };
                intResult = DU.executeprocedure("USP_IVR_SaveTranTotal", sparam);
            }
            else if (hidTranFormatType.Value == "True")
            {
                int intResult = 0;
                SqlParameter[] sparam ={
                           new SqlParameter("@TRNID",strTranId.ToString()),
                           new SqlParameter("@ITEMTOTAL",txtTotalItemBillLevel.Text),                           
                           new SqlParameter("@TAXABLEAMT",txtTaxableAmtBillLevel.Text),
                           new SqlParameter("@TAXAMT",txtVatBillLevel.Text),                           
                           new SqlParameter("@RNDOFFAMT",txtRoundOffBillLevel.Text),
                           //new SqlParameter("@RNDOFFAMT_LID",ddlRoundOff.SelectedValue),
                           new SqlParameter("@TRNTOTAL",txtGrandTotalBillLevel.Text)                                                                                                                 
                       };
                intResult = DU.executeprocedure("USP_IVR_SaveTranTotal", sparam);
            }
        }
    }

    protected void InsertTranOthers(string tranId)//Insert Transaction Others Information into IVR_Tran_Others database Table
    {
        if (tranId.ToString() != string.Empty)
        {
            SqlParameter[] sparam =
                                    {
                                        new SqlParameter("@TRNID",tranId.ToString()),
                                        new SqlParameter("@PREPAREDBY",txtPreparedBy.Text.Trim()),
                                        new SqlParameter("@CHECKEDBY",txtCheckedBy.Text.Trim()),
                                        new SqlParameter("@AUTHRSDSIGNAT",txtSignatory.Text.Trim()),
                                        new SqlParameter("@NARRATION",txtNarration.Text.Trim()),
                                        new SqlParameter("@REMARKS",txtRemark.Text.Trim())                                        
                                    };
            DU.executeprocedure("USP_IVR_SaveTranOthers", sparam);
        }
    }
   
    protected void InsertTranDelivery(string tranId)//Insert Delivery Information into IVR_Tran_Delivery
    {
        if (tranId.ToString() != string.Empty)
        {
            string strDeliveryDate = "1/1/1900";
            if (txtDeliveryDate.Text != string.Empty)
            {
                string[] str = txtDeliveryDate.Text.ToString().Split('/');
                strDeliveryDate = str[1] + "/" + str[0] + "/" + str[2];
            }
            SqlParameter[] sparam =
                                    {
                                        new SqlParameter("@TRNID",tranId.ToString()),
                                        new SqlParameter("@MODEOFDELIVERY",txtDeliveryMode.Text.Trim()),
                                        new SqlParameter("@DELIVERYDATE",strDeliveryDate) ,
                                        new SqlParameter("@DOCUMENTNUM",txtDocumentNo.Text.Trim()),
                                        new SqlParameter("@VEHICLENUM",txtVehicle.Text.Trim()),
                                        new SqlParameter("@TRANSPORTNAME",txtTransporter.Text.Trim())                                                                              
                                    };
            DU.executeprocedure("USP_IVR_SaveTranDelivery", sparam);
        }
    }
}

//error 1st (deleting a update panel) , 2nd(line no 1089 {comment on those line),  3rd(line no 917(error BindJavaScriptBillLevel {on btnQtyBillLevel.attribute)

